package com.xywh.wanandroid.compose_ui.login

import android.app.Activity
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Face
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import com.xywh.composeLibrary.view.XxButton
import com.xywh.composeLibrary.view.XxTextField
import com.xywh.composeLibrary.view.XxTopAppBarCenter
import com.xywh.wanandroid.R
import com.xywh.wanandroid.ext.hideSoftKeyboard
import com.xywh.wanandroid.util.CacheUtil
import com.xywh.wanandroid.viewmodel.LoginComposeViewModel

@Composable
fun LoginScreen(
    loginComposeViewModel: LoginComposeViewModel = hiltViewModel(),
    updateMainScreenCollectListData: () -> Unit,
    navigateUp: () -> Unit,
    navigateRegister: () -> Unit,
) {
    val activity = LocalContext.current as Activity
    var username by remember { mutableStateOf("") }
    var password by remember { mutableStateOf("") }

    if (CacheUtil.isLogin()) {
        val user = CacheUtil.getUser()
        user?.let {
            username = it.username
//            password = it.password
        }
    }

    XxTopAppBarCenter(title = stringResource(R.string.login_title), navigationIconOnClick = {
        navigateUp.invoke()
    }, content = { innerPadding ->

        Column(
            Modifier
                .padding(top = innerPadding.calculateTopPadding())
                .padding(20.dp, 0.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            Column(Modifier.fillMaxWidth()) {
                Text(
                    text = stringResource(R.string.user_login),
                    color = Color.Black,
                    fontSize = 18.sp,
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier
                        .padding(top = 40.dp)
                )
                Text(
                    text = stringResource(R.string.user_login_tip),
                    color = Color.Gray,
                    fontSize = 14.sp,
                    textAlign = TextAlign.Center,
                    modifier = Modifier
                        .padding(top = 10.dp)
                )
            }

            XxTextField(
                defaultTextValue = username,
                hint = stringResource(id = R.string.login_username_hint),
                top = 40.dp,
                textSize = 14.sp,
                onTextChange = {
                    username = it
                }
            )
            XxTextField(
                defaultTextValue = password,
                hint = stringResource(id = R.string.login_pwd_hint),
                top = 20.dp,
                textSize = 14.sp,
                onTextChange = {
                    password = it
                },
                leadingIcon = Icons.Filled.Face,
                visualTransformation = PasswordVisualTransformation()
            )

            XxButton(
                textColor = Color.White,
                textSize = 14.sp,
                textValue = stringResource(id = R.string.login_btn),
                top = 20.dp,
                height = 50.dp,
                onClick = {
                    hideSoftKeyboard(activity)
                    loginComposeViewModel.loginWanAndroid(
                        username,
                        password,
                        updateMainScreenCollectListData,
                        navigateUp
                    )
                }
            )
            Box(modifier = Modifier.fillMaxWidth()) {

                Text(
                    text = stringResource(R.string.go_register),
                    color = Color.Gray,
                    fontSize = 14.sp,
                    textAlign = TextAlign.Center,
                    modifier = Modifier
                        .padding(top = 10.dp)
                        .align(Alignment.BottomEnd)
                        .clickable {
                            navigateRegister.invoke()
                        }
                )
            }
        }
    })
}

